package com.zy.pro.system.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.system.SystemCnt;
import com.zy.pro.system.model.Employee;
import com.zy.pro.system.service.EmployeeService;
import com.zy.pro.system.web.form.EmployeeSearchForm;
import com.zy.pro.user.model.User;
import com.zy.utils.MethodUtil;
import com.zy.utils.SessionUtils;
import com.zy.utils.UploadHelper;
/**
 * 系统管理员管理控制器
 *
 */
@Controller
@RequestMapping("/control/system/employeeController")
public class EmployeeController extends BaseController {

	private static final Logger logger = Logger.getLogger(EmployeeController.class);
	
	/**
	 * 系统管理员管理服务类
	 */
	@Resource(name = "employeeServiceImpl")
	private EmployeeService employeeService;
	
	/**
	 * 管理员登录
	 * @param user
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public void login(Employee employee, ModelMap map,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		try {
			String message = "";
			if(employee != null && employee.getEmployeeName() != null && !"".equals(employee.getEmployeeName().trim()) && employee.getPassword() != null && !"".equals(employee.getPassword().trim())){
				employee.setPassword(MethodUtil.MD5(employee.getPassword()));
				Employee employeeInfo = employeeService.findEmployeeNameAndPassword(employee);
				if(employeeInfo != null && employeeInfo.getEmployeeStatus() != 2){
					//将店家信息放置到session当中
					SessionUtils.setEmployee(request, employeeInfo);
					SessionUtils.setRoleType(request, employeeInfo.getRoleType());
					response.getWriter().print(employeeInfo.getEmployeeId().toString()+"success");
				}else{
					if(employeeInfo == null){
						message = "该用户不存在或密码错误";
					}else if(employeeInfo.getEmployeeStatus() == 2){
						message = "用户已经被冻结,请联系管理员";
					}
					response.getWriter().print(message);
				}
			}else{
				if(employee != null){
					if(employee.getEmployeeName() == null || "".equals(employee.getEmployeeName().trim())){
						message = "用户名不能为空";
					}else{
						message = "密码不能为空";
					}
				}else{
					message = "用户名和密码不能为空";
				}
				response.getWriter().print(message);
			}
		} catch(Exception e){
			logger.error(e);
		}
	}
	
	/**
	 * 管理员登录后展示后台管理系统首页
	 * @return
	 */
	@RequestMapping("/displayIndex")
	public String displayIndex(ModelMap map,HttpServletRequest request){
		//管理员登录后在index页放置角色类型值(退出登录时需要根据角色类型值判断返回的页面)
		map.addAttribute("emplRoleType", "3");
		return "/control/system/sysIndex";
	}
	
	
	/**
	 * 列出系统管理员管理
	 */
	@RequestMapping("/list")
	public String list(EmployeeSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Employee> pageView = new PageView<Employee>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(employeeService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加系统管理员管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Employee employee, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加系统管理员管理
	 */
	@RequestMapping("/add")
	public String add(Employee employee, ModelMap map) {
		try {
			//TODO 设置employee  eg:主键信息
			
			employeeService.save(employee);
			return list(new EmployeeSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑系统管理员管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(EmployeeSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Employee employee = employeeService.find(form.getId());
			//eg:map.addAttribute("entity", employee);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑系统管理员管理
	 */
	@RequestMapping("/edit")
	public String edit(Employee Employee, ModelMap map) {
		try {
			//TODO:处理系统管理员管理
			
			
			employeeService.updateBySelective(Employee);
			return list(new EmployeeSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个系统管理员管理
	 */
	@RequestMapping("/deleteemployee")
	public String delete(EmployeeSearchForm form, ModelMap map) {
		try {
			//TODO  eg:employeeService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除系统管理员管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(EmployeeSearchForm form, ModelMap map) {
		try {
//			TODO  eg:employeeService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	
	/**
	 * 替换下拉框内容(字段制定/字段修改界面)
	 * @param selectContentFile
	 * @param colId 字段ID
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/uploadFile")
	public void uploadFile(@RequestParam("imageFile") MultipartFile imageFile,HttpServletRequest request,HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			String message = "";//返回信息
			if(!imageFile.isEmpty()){//文件不为空
				String imagePath = UploadHelper.upload(imageFile, "yibu", request, logger);
				response.getWriter().print(imagePath);
			}
			//message = "图片不能为空";
			response.getWriter().print(message);
		} catch (Exception e) {
			logger.error("上传图片执行错误..");
		}
	}
}
